﻿using System;
using System.Data;
using System.Text;
using Microsoft.SharePoint;

namespace QueryDemo.ResultProviders
{
	/// <summary>
	/// Base class for all search and query operations.
	/// </summary>
	public abstract class BaseResultProvider
	{
		public QueryType TypeOfQuery { get; set; }
		public string QueryName { get; set; }
		public string ListTitle { get; set; }
		public Guid ListId { get; set; }
		public SPWeb Web { get; set; }
		public SPSite Site { get; set; }
		public int ServerTemplate { get; set; }
		public SPViewFieldCollection ViewFields { get; set; }
		public string CamlQuery { get; set; }
		public int RowLimit { get; set; }

		/// <summary>
		/// Returns whether a result provider gives results from across the site collection of just within a single web.
		/// </summary>
		public abstract bool CrossSiteCapable { get; }

		/// <summary>
		/// Initialisation steps for result provider.
		/// </summary>
		public abstract void Init();

		/// <summary>
		/// Execution steps for result provider.
		/// </summary>
		/// <returns>Retrieved results.</returns>
		public abstract DataTable ExecuteQuery();

		/// <summary>
		/// Converts list of fields into FieldRef XML property format.
		/// </summary>
		/// <returns>Concatenated FieldRefs.</returns>
		protected string GetViewFieldCaml()
		{
			var builder = new StringBuilder();

			foreach (string field in ViewFields.ToStringCollection())
			{
				builder.Append("<FieldRef Name='" + field + "'/>");
			}

			return builder.ToString();
		}
	}
}
